<template>
	<view>
		<view class="main bgw p30">
			<p class="title">付款金额</p>
			<view class="money ">
				<view>￥</view>
				<input @blur="numCheCking()" maxlength="7" type="number" v-model="money" placeholder="请输入您要收取的金额" />
			</view>
			<textarea maxlength="20" class="text-df" v-model="remarks" placeholder="添加备注（20字以内）" />
			</view>
		<view class="btn p30"><button @tap="checking()" class="btnbg">确认收款</button></view>
	</view>
</template>

<script src="./base64.js"></script>
<script>
import { recharge, cardPay, scanCode } from '@/api/api.js';
import { Base64 } from '@/components/base64.js';
export default {
	data() {
		return {
			shakeState:0,
			phone: '',
			money:'',
			remarks: ''
		};
	},
	onShow() {
		this.shakeState = 0
		this.remove()
	}, 
	onHide() {
		this.remove()
	},
	methods: { 
		remove() {
			this.phone = '';
			this.money = '';
			this.remarks = '';
		},
		numCheCking() {
			this.money = Math.round(this.money * 100) / 100;
			if (this.money == 0) {
				this.money = '';
			}
		},
		checking() { 
			this.money = Math.round(this.money * 100) / 100;
			if (this.money == 0) {
				this.money = '';
			}
			try {
				if (this.money == '') throw '请填写金额';
				if (this.money <= 0) throw '金额要大于0';
			} catch (err) {
				this.toa(err);
				return;
			}
			this.send();
		},
		send() {
			uni.scanCode({
				success: res => {
					console.log(res);
					let codess = Base64.decode(res.result);
					codess = codess.substr(7);
					let data = {
						code: codess,
						money: this.money,
						remarks: this.remarks
					};
					if (codess == null) {
						this.toa('请重新扫码');
					} else if (res.result.length < 20) {
						this.toa('请重新扫码');
					} else {
						if(this.shakeState == 0){
							this.shakeState = 1
							cardPay(data).then(ress => {
								let parameter = {
									code: ress.code,
									msg: ress.msg,
									money: this.money,
									remarks: this.remarks
								};
								uni.setStorageSync('payData', parameter);
								uni.navigateTo({
									url: '../vipPayType/vipPayType'
								});
							});		
							this.shakeState = 0
						} 
					}
				}
			});
		},
		toa(txt) {
			uni.showToast({
				title: txt,
				icon: 'none'
			});
		}
	}
};
</script>

<style lang="less" scoped>
@import './index.less';
</style>
